package com.example.administrator.bdsjws.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.example.administrator.bdsjws.R;
import com.example.administrator.bdsjws.activities.base.BaseActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import butterknife.BindView;
import butterknife.OnClick;

public class UserActivity extends BaseActivity {

    @BindView(R.id.tool_bar)
    Toolbar toolbar;
    @BindView(R.id.gengxinbtn)
    LinearLayout gengxinbtn;
    @BindView(R.id.aaaaa)
    TextView aaaaa;
    @BindView(R.id.activity_user)
    LinearLayout activityUser;

    Handler handler=new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            Toast.makeText(UserActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
            Log.d("aaaaaaaaaa",msg.obj.toString());
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_user;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @OnClick({R.id.aaaaa, R.id.gengxinbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.aaaaa:
                Intent intent = new Intent(UserActivity.this, User2Activity.class);
                startActivity(intent);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url=new URL("http://118.89.191.15/json/aa.json");
                            HttpURLConnection conn= (HttpURLConnection) url.openConnection();

                            InputStream inputStream = conn.getInputStream();
                            BufferedReader bu=new BufferedReader(new InputStreamReader(inputStream));
                            Message m=handler.obtainMessage();
                            m.obj=bu.readLine();
                            handler.sendMessage(m);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
                break;
            case R.id.gengxinbtn:
                Intent intent1 = new Intent(UserActivity.this, Activity_Update.class);
                startActivity(intent1);
                break;
        }
    }
}
